package com.dapangdong.testdemo.controller.test;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.dapangdong.testdemo.Entiy.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * </p>
 *
 * @author : yhd
 * @program: gulimall
 * @since : 2023-04-10 09:55
 */
@RequiredArgsConstructor
@Slf4j
@Api(tags = "Redis")
@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    private final RedisTemplate<String, Object> redisTemplate;

    @ApiOperation(value = "将数据写入缓存")
    @GetMapping("/putRedis")
    public Object putRedis(){
        String key = "myKey";
        String key2 = "code:"+LocalDate.now();
        Object value;
        // 先检查是否存在缓存
//        if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {
//            // 从缓存中获取数据
//            value  = redisTemplate.opsForValue().get(key);
//        } else {
            // 执行业务逻辑，并获取数据
            User user = User.builder().id(1L).name("sdkl")
                    .localDateTime(LocalDateTime.now())
                    .localDate(LocalDate.now())
                    .date(new Date())
                    .dateTime(DateTime.now())
                    .build();
            value = user;
//            value = "LocalDateTime.now().toString()富士康就";
            // 将数据写入缓存
            redisTemplate.opsForValue().set(key, value, 5 ,TimeUnit.SECONDS);
            redisTemplate.opsForValue().increment(key2);
//        }

        log.info("写入缓存");
        // 从缓存中获取数据
        value  = redisTemplate.opsForValue().get(key);
        User toBean = BeanUtil.toBean(value,User.class);
        return toBean;
    }


    public static void main(String[] args) throws UnsupportedEncodingException {

        String s = StringUtils.leftPad(String.valueOf(123456), 5, '0');
        System.out.println(s);

        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "我爱你ლ(′◉❥◉｀ლ)";
        User user = User.builder().id(1L).name("sdkl")
                .localDateTime(LocalDateTime.now())
                .localDate(LocalDate.now())
                .date(new Date())
                .dateTime(DateTime.now())
                .build();
        final byte[] textByte = text.getBytes(StandardCharsets.UTF_8);
        //编码
        final String encodedText = encoder.encodeToString(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decode(encodedText), StandardCharsets.UTF_8));


    }


}
